<?php
require('setup.php');
include 'db.php';
include 'ajax-functions.php';
$smarty = new Smarty_pigier();
$username=null;
$password=null;
$usernametype=null;
session_start();
if (isset($_GET['inscription']) && $_GET['inscription']==1 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==0){
	include 'forminscription.php';
}
else if (isset($_GET['listcours']) && $_GET['listcours']==1 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==1){
	$conn = connectDB();
	$result=selectAllcours();
	$elements = array();
	while ($row=mysql_fetch_array($result)){
		$listpdf=selectCourfiles($row['id_cours']);
		$list = array();
		while ($srow=mysql_fetch_array($listpdf))
		{
			array_push($list, $srow);
		}
		$row['listpdf']=$list;

		array_push($elements, $row);
	}
	$smarty->assign('elements', $elements);
	$menu  = file_get_contents('templates/menustudent.tpl');
	$smarty->assign('menu',$menu);
}
else if (isset($_GET['listtravaux']) && $_GET['listtravaux']==1 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==1){
	$conn = connectDB();
	$result=selectAllcours();
	$elements = array();
	while ($row=mysql_fetch_array($result)){
		$sresult=selectTravauxByCour($row['id_cours']);
		while ($srow=mysql_fetch_array($sresult)){
			$srow['nomcour']=$row['nomcours'];
			$listpdf=selectCourfilesByID($srow['id_fichier']);
			while ($ssrow=mysql_fetch_array($listpdf))
			{
				$srow['nomfichier']=$ssrow['nomfichier'];
				$srow['path']=$ssrow['path'];
			}
				array_push($elements, $srow);
		}
	}
	$smarty->assign('elements', $elements);
	$menu  = file_get_contents('templates/menustudent.tpl');
	$smarty->assign('menu',$menu);
}
else if (isset($_GET['goto']) && $_GET['goto']==1 ){
	include 'inscription.php';
}
else if  (isset($_GET['connect']) && $_GET['connect']==1){
	include 'connect.php';
}
else if (isset($_GET['updatecours']) && $_GET['updatecours']==1 && isset($_GET['filiere']) && isset($_GET['annee']))
{
	include 'updatecour.php';
}
else if(isset($_GET['connexion']) && $_GET['connexion']==1 ){
	include 'login.php';
}
else if(isset($_GET['profil']) && $_GET['profil']==1)
{
	include 'profile.php';
}
else if(isset($_GET['passwordforgotten']) && $_GET['passwordforgotten']==1 ){
	include 'passwordforgotten.php';
}
else if(isset($_GET['deconnect']) && $_GET['deconnect']==1 ){
	include 'deconnect.php';
}
else if( isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==0 && isset($_GET['editpage'])) {
	include 'editpage.php';
}
else if( isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==0 && isset($_GET['insert'])&& isset($_GET['edit']) && $_GET['edit']==1 && $_GET['insert']==1) {
	include 'updatepage.php';
}
elseif (isset($_GET['upload']) && $_GET['upload']==1 && $_POST['cours']){
	include 'uploadfilecour.php';
	setcontentcours($smarty);
}

else if(isset($_GET['cours']) && $_GET['cours']==1 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==2){
	//$bodydinamiccontent =  file_get_contents('templates/cours.tpl');
	setcontentcours($smarty);
}
else if(isset($_GET['traveau']) && $_GET['traveau']==1 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==2){
	//$bodydinamiccontent =  file_get_contents('templates/cours.tpl');
	$conn = connectDB();
	if (isset($_GET['delete'])) {
		$resulttravaux=selectTravauById($_GET['id']);
		
		$row = mysql_fetch_array($resulttravaux);
		deleteFichier($row['id_fichier']);
		deleteTravaux($_GET['id']);
		$_GET['id']=null;
	}
	
	$result=selectProfCour(selectIdcategoryFromProfTable($_SESSION['_id']));
	$arr=array();
	while ($row = mysql_fetch_array($result)) {
		$arr[$row['id_cours']]=$row['nomcours'];

	}
	$resulttravaux=selectTravauPourPro($_SESSION['_id']);
	$arraytravaux=array();
	while ($row = mysql_fetch_array($resulttravaux)) {

		$noncours = selectCourById($row['id_cours']);
		$fichier=selectFichierById($row['id_fichier']);
		$rowfichier=mysql_fetch_array($fichier);
		$row['nomcour']=$noncours;
		$row['nomfichier']=$rowfichier['nomfichier'];
		$row['path']=$rowfichier['path'];
		array_push($arraytravaux, $row);
		//array_push($arr, $row['nomcours']);
	}
	$smarty->assign('donnetravaux', $arraytravaux);
	$smarty->assign('coursprof',$arr);
	$menu  = file_get_contents('templates/menuteacher.tpl');
	$smarty->assign('menu',$menu);
	
}
else if(isset($_GET['traveau']) && isset($_GET['id'])   && isset($_GET['idcour']) && $_GET['traveau']==4 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==2){

	$titre = $_POST['titre'];
	$desc = $_POST['desc'];
	$type = $_POST['type'];
	$id_prof = $_SESSION['_id'];
	$id_cours = $_POST['cours'];

	$conn=connectDB();
	$id_file=0;
	//($nomtravaux,$typedetravaux,$id_prof,$desc,$id_cours)
	if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
	{

		$filename =strtolower(basename($_FILES['uploaded_file']['name']));
		$filenametosave = substr($filename, 0,strrpos($filename, '.'));
		$ext = substr($filename, strrpos($filename, '.') + 1);

		if ((($ext == "pdf")||($ext == "pdf")) && ($_FILES["uploaded_file"]["size"] < 5000000))
		{
			//Determine the path to which we want to save this file
			$typefichier=$ext;
			$ext=".".$ext;
			$newfilename= md5(rand() * time());
			$pathtosave='uploadedfiles/'.$newfilename.$ext;
			$path = dirname(__FILE__).'/uploadedfiles/'.$newfilename.$ext;
			$path = str_replace("\\", "/", $path);
			if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$path)))
			{
				$conn=connectDB();
				$vl=insertfichier($filenametosave,$id_cours,$pathtosave,$typefichier);

				$id_file=selectFileWithCourAndName($filenametosave,$id_cours,$pathtosave);
				$res=mysql_query($vl);
				$smarty->assign('uploadfeedback', "fichier upload avec success");


				//setcontentcours($smarty);

			}
			else
			{

				$smarty->assign('uploadfeedback',"fichier unupload");

			}
		} else {
			$smarty->assign('uploadfeedback',"Size of file");
		}

	}



	if (updateTravaux($id_file, $titre, $type, $desc, $_GET['id'])) {
		$smarty->assign('feedback',"Travaux mise a jour avec  succee !!!!");

	}
	$menu  = file_get_contents('templates/menuteacher.tpl');
	$smarty->assign('menu',$menu);
	$result=selectProfCour(selectIdcategoryFromProfTable($_SESSION['_id']));
	$arr=array();
	while ($row = mysql_fetch_array($result)) {
		$arr[$row['id_cours']]=$row['nomcours'];

	}
	$smarty->assign('coursprof',$arr);

	$resulttravaux=selectTravauPourPro($_SESSION['_id']);
	$arraytravaux=array();
	while ($row = mysql_fetch_array($resulttravaux)) {

		$noncours = selectCourById($row['id_cours']);
		$fichier=selectFichierById($row['id_fichier']);
		$rowfichier=mysql_fetch_array($fichier);
		$row['nomcour']=$noncours;
		$row['nomfichier']=$rowfichier['nomfichier'];
		$row['path']=$rowfichier['path'];

		array_push($arraytravaux, $row);
		//array_push($arr, $row['nomcours']);
	}
	$smarty->assign('donnetravaux', $arraytravaux);

}



else if(isset($_GET['traveau']) && isset($_GET['id']) && $_GET['traveau']==3 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==2){
	//$bodydinamiccontent =  file_get_contents('templates/cours.tpl');
	$conn = connectDB();
	$result=selectProfCour(selectIdcategoryFromProfTable($_SESSION['_id']));
	$arr=array();
	while ($row = mysql_fetch_array($result)) {
		$arr[$row['id_cours']]=$row['nomcours'];

	}


	$smarty->assign('coursprof',$arr);

	$resulttravaux=selectTravauById($_GET['id']);
	$arraytravaux=array();
	$row = mysql_fetch_array($resulttravaux);
	$smarty->assign('titretravaux',$row['nomtravaux']);
	$smarty->assign('tipetravaux',$row['typetravaux']);
	$smarty->assign('desctravaux',$row['description']);

	if (isset($_GET['deletefile']) && $_GET['deletefile']=1 ) {
		deleteFichier($row['id_fichier']);
		updateFichierTravaux(0,$_GET['id']);
	}


	$noncours = selectCourById($row['id_cours']);
	$menu  = file_get_contents('templates/menuteacher.tpl');
	$smarty->assign('menu',$menu);

	$fichier=selectFichierById($row['id_fichier']);
	if ($fichier) {
		$rowfichier=mysql_fetch_array($fichier);
		$smarty->assign('nomfichier',$rowfichier['nomfichier']);
		$smarty->assign('path',$rowfichier['path']);

	}
	$smarty->assign('idtravaux',$_GET['id']);
	$smarty->assign('idcour',$_GET['idcour']);
	$resulttravaux=selectTravauPourPro($_SESSION['_id']);
	$arraytravaux=array();
	while ($row = mysql_fetch_array($resulttravaux)) {

		$noncours = selectCourById($row['id_cours']);
		$fichier=selectFichierById($row['id_fichier']);
		$rowfichier=mysql_fetch_array($fichier);
		$row['nomcour']=$noncours;
		$row['nomfichier']=$rowfichier['nomfichier'];
		$row['path']=$rowfichier['path'];
		array_push($arraytravaux, $row);
		//array_push($arr, $row['nomcours']);
	}
	$smarty->assign('donnetravaux', $arraytravaux);
}

else if(isset($_GET['traveau']) && $_GET['traveau']==2 && isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==2){

	$titre = $_POST['titre'];
	$desc = $_POST['desc'];
	$type = $_POST['type'];
	$id_prof = $_SESSION['_id'];
	$id_cours = $_POST['cours'];
	$conn=connectDB();
	$id_file=0;
	//($nomtravaux,$typedetravaux,$id_prof,$desc,$id_cours)
	if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
	{
		$filename =strtolower(basename($_FILES['uploaded_file']['name']));
		$filenametosave = substr($filename, 0, strrpos($filename, '.'));
		$ext = substr($filename, strrpos($filename, '.') + 1);

		if ((($ext == "pdf")||($ext == "pdf")) && ($_FILES["uploaded_file"]["size"] < 5000000))
		{
			//Determine the path to which we want to save this file
			$typefichier=$ext;
			$ext=".".$ext;
			$newfilename= md5(rand() * time());
			$pathtosave='uploadedfiles/'.$newfilename.$ext;
			$path = dirname(__FILE__).'/uploadedfiles/'.$newfilename.$ext;
			$path = str_replace("\\", "/", $path);
			if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$path)))
			{
				$conn=connectDB();
				$vl=insertfichier($filenametosave,$id_cours,$pathtosave,$typefichier);

				$id_file=selectFileWithCourAndName($filenametosave,$id_cours,$pathtosave);
				$res=mysql_query($vl);
				$smarty->assign('uploadfeedback', "fichier upload avec success");
				//setcontentcours($smarty);
			}
			else
			{

				$smarty->assign('uploadfeedback',"fichier unupload");

			}
		} else {
			$smarty->assign('uploadfeedback',"Size of file");
		}

	}
	if (insertFTravaux($titre,$type,$id_prof,$desc,$id_cours,$id_file)) {
		$smarty->assign('feedback',"Travaux ajouter succee !!!!");

	}
	$menu  = file_get_contents('templates/menuteacher.tpl');
	$smarty->assign('menu',$menu);
	$result=selectProfCour(selectIdcategoryFromProfTable($_SESSION['_id']));
	$arr=array();
	while ($row = mysql_fetch_array($result)) {
		$arr[$row['id_cours']]=$row['nomcours'];

	}
	$smarty->assign('coursprof',$arr);

	$resulttravaux=selectTravauPourPro($_SESSION['_id']);
	$arraytravaux=array();
	while ($row = mysql_fetch_array($resulttravaux)) {

		$noncours = selectCourById($row['id_cours']);
		$fichier=selectFichierById($row['id_fichier']);
		$rowfichier=mysql_fetch_array($fichier);
		$row['nomcour']=$noncours;
		$row['nomfichier']=$rowfichier['nomfichier'];
		$row['path']=$rowfichier['path'];

		array_push($arraytravaux, $row);
		//array_push($arr, $row['nomcours']);
	}
	$smarty->assign('donnetravaux', $arraytravaux);
}
else if(isset($_GET['projet']) && $_GET['projet']==1 &&  isset($_SESSION['_type']) && $_SESSION['_type']==0){

	if (isset($_GET['editprojet']) && $_GET['editprojet'] == 1) {
		$conn = connectDB();
		$row = mysql_fetch_array(selectprojetById($_GET['id']));
		$smarty->assign('titre',$row['nomprojet']);
		$smarty->assign('desc',html_entity_decode($row['description']));
		$tt = "index.php?projet=1&update=".$row['id_projet'];
		$smarty->assign('action',$tt);
	}
	else if (isset($_GET['update'])) {
		$conn = connectDB();
		updateProjet($_POST['titre'], mysql_real_escape_string($_POST['elm1']), $_GET['update']);
		header("location:index.php?projet=2");
	}
	else {
		if (isset($_POST['titre']) &&  isset($_POST['elm1']) ) {
			$conn = connectDB();
			insertProjet($_POST['titre'], mysql_real_escape_string($_POST['elm1']));
			header("location:index.php?projet=2");
		}
	}
	$menu  = file_get_contents('templates/menuadmin.tpl');
	$smarty->assign('menu',$menu);
}

else if(isset($_GET['projet']) && $_GET['projet']==2 && isset($_SESSION['_type'])){
	$conn = connectDB();
	$result = selectprojets();

	$arrayrojet =  array();
	while ($row = mysql_fetch_array($result)) {
		$row['contenu']=html_entity_decode($row['description']);
		array_push($arrayrojet, $row);
	}
	if ($_SESSION['_type']==0) {
		$menu  = file_get_contents('templates/menuadmin.tpl');
	}
	else if ($_SESSION['_type']==1){
		$menu  = file_get_contents('templates/menustudent.tpl');
	}
	else if ($_SESSION['_type']==2){
		$menu  = file_get_contents('templates/menuteacher.tpl');
	}
	
	
	$smarty->assign('menu',$menu);
	$smarty->assign("lesprojes",$arrayrojet);
}
else {

	if ( isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==0) {
		//	$rightcontent  = file_get_contents('templates/connect.tpl');
		$menu  = file_get_contents('templates/menuadmin.tpl');
		$string = "Bievenue admin";
		$smarty->assign('sidebarfirstelement',"");
		$conn = connectDB();
		$smarty->assign('bodycontent',selectcontent("home"));
	}
	else if ( isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==1) {
		//	$rightcontent  = file_get_contents('templates/connect.tpl');
		$menu =  file_get_contents('templates/menustudent.tpl');
		$string = "Bievenue Student";
		$smarty->assign('sidebarfirstelement',"");
	}
	else if ( isset($_SESSION['_login']) && isset($_SESSION['_pass']) && $_SESSION['_type']==2) {
		//	$rightcontent  = file_get_contents('templates/connect.tpl');
		$menu  = file_get_contents('templates/menuteacher.tpl');
		$string = "Bievenue Professeur";
		$smarty->assign('sidebarfirstelement',"");
		$smarty->assign('TITLE',"Pigi web site");
	}
	else {
		$rightcontent  = file_get_contents('templates/connect.tpl');
		$menu  = file_get_contents('templates/menu.tpl');
		$smarty->assign('sidebarfirstelement',$rightcontent);
		$string="Bievenue Visiteur";
		//$smarty->assign('bodycontent',"Presentation");

	}
	$smarty->assign('TITLE',"Pigi web site");
	$smarty->assign('menu',$menu);
	$smarty->assign('TITLE',$string);
	$conn = connectDB();
	$smarty->assign('bodycontent',selectcontent("home"));
	$smarty->assign('bodytitle',"Home page");
}
function setcontentcours($smarty){
	$menu  = file_get_contents('templates/menuteacher.tpl');
	$smarty->assign('menu',$menu);

	$conn = connectDB();
	$result=selectfiliere();
	$arr=array();
	while ($row = mysql_fetch_array($result)) {
		$arr[$row['id_filiere']]=$row['nomfiliere'];

		//array_push($arr,$row['nomfiliere'] => );
	}
	$smarty->assign('selectfl', $arr);

	$conn = connectDB();
	$result=selectanne();
	$arr=array();
	while ($row = mysql_fetch_array($result)) {
		$arr[$row['id_niveau']]=$row['anne'];
		//array_push($arr, $row['anne']);
	}
	$smarty->assign('selectan', $arr);

}
function uploadFile(){

	if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
	{

		$filename =strtolower(basename($_FILES['uploaded_file']['name']));
		$filenametosave = substr($filename, 0,strrpos($filename, '.'));

		$ext = substr($filename, strrpos($filename, '.') + 1);

		if ((($ext == "pdf")||($ext == "pdf")) && ($_FILES["uploaded_file"]["size"] < 500000))
		{
			//Determine the path to which we want to save this file
			$typefichier=$ext;
			$ext=".".$ext;
			$pathtosave='/uploadedfiles/'.$newfilename.$ext;
			$path = dirname(__FILE__).'uploadedfiles/'.$newfilename.$ext;
			$path = str_replace("\\", "/", $path);
			if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$path)))
			{
				$conn=connectDB();
				$vl=insertfichier($filenametosave,$fkcours,$pathtosave,$typefichier);
				$res=mysql_query($vl);
				$smarty->assign('uploadfeedback', "fichier upload avec success");
				setcontentcours($smarty);

			}
			else
			{
				$smarty->assign('uploadfeedback',"fichier unupload");

			}
		} else {

		}
	} else {
		echo "Error! File is not uploaded!";
		print('<p><a href="upload_file.php">Back</a></p>');
	}
}
$smarty->display('index.tpl');
?>

